# -*- coding: utf-8 -*-
import sys
import os
import linecache
import random
from pixivpy3 import AppPixivAPI
# import cv2
# import pixivpy_async.sync
# from pixivpy_async import AppPixivAPI
#import gc
#import pixivpy_async.sync
# from pathlib import Path
#from pixivpy_async import AppPixivAPI


sys.dont_write_bytecode = True

# _TOKEN = "uXooTT7xz9v4mflnZqJUO7po9W5ciouhKrIDnI2Dv3c"



# def _login(aapi):
#     # await aapi.login(_USERNAME, _PASSWORD)
#     aapi.login(refresh_token=_TOKEN)

def _dl(d):
    _TOKEN = "uXooTT7xz9v4mflnZqJUO7po9W5ciouhKrIDnI2Dv3c"
    aapi = AppPixivAPI()
    aapi.auth(refresh_token=_TOKEN)
    directory = "E:\\学习相关\\C++\\testCode\\mirai-cpp-template\\temp_img"
    if not os.path.exists(directory):
        print("不存在文件夹")
        os.makedirs(directory)
    if d == 1:
        _file = "E:/学习相关/C++/testCode/mirai-cpp-template/link_list/d_links.txt"
    if d == 2:
       _file = "E:/学习相关/C++/testCode/mirai-cpp-template/link_list/w_links.txt"
    if d == 3:
        _file = "E:/学习相关/C++/testCode/mirai-cpp-template/link_list/m_links.txt"
    if d == 4:
        _file = "E:/学习相关/C++/testCode/mirai-cpp-template/link_list/rd_links.txt"
    if d == 5:
        _file = "E:/学习相关/C++/testCode/mirai-cpp-template/link_list/rw_links.txt"
    if os.path.exists(_file):
        print(_file, " is exists!")
        sz = os.path.getsize(_file)
        if not sz:
            print(_file, " is empty!")
        else:
            count = 0
            with open(_file,'r') as f:
                for index, line in enumerate(f):
                    count += 1
            print(count)
            n = int(random.randint(1,count))
            print(n)
            link = linecache.getline(_file,n)
            print(link)
            # _id_name = _id.replace('\n', '')
            # print(_id_name)
            # link = 'https://pixiv.cat/'+_id_name+'.jpg'
            # print("5")
            # print(aapi.download(link,path=directory,name = _id_name+'.jpg'))

            aapi.download(link,path=directory,name = (str(n)+".Webp"))
            # old_img_address = directory+"\\"+str(n)
            # print(old_img_address)
            # a = cv2.imread(old_img_address)
            # new_img_address = directory+"\\"+str(n)+".png"
            # print(new_img_address)
            # cv2.imwrite(new_img_address,a)
            # filename=os.listdir(directory)[0]
            # os.remove(directory+"\\"+str(n))
            # print(filename)
            return str(n)+""

            # return "233"

            # if not filename.endswith('g'):
            #     os.remove(directory+'/'+filename)
            #     print('只获取该链接第一张')
            #     link = 'https://pixiv.cat/'+_id_name+'-1'+'.png'
            #     print(link)
            #     print(aapi.download(link,path=directory,name = _id_name+'.jpg'))
            #     filename=os.listdir(directory)[0]
            #     return filename
                    
            # else:
            #     print('ok')
            #     return filename
    else:
        print(_file, " is not exists!")

# def _main(d):
#     # _login(aapi)
#     dl(d)

# if __name__ == '__main__':
#     print(_dl(3))